package com.example.startscreen;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import android.util.Log; 

public class CustomItemizedOverlay extends ItemizedOverlay<MyOverlayItem> {
	
	private ArrayList<MyOverlayItem> mapOverlays = new ArrayList<MyOverlayItem>();
	
	private Context context;
	private int mTextSize;
	
	public CustomItemizedOverlay(Drawable defaultMarker) {
		  super(boundCenterBottom(defaultMarker));
	}
	

	public CustomItemizedOverlay(Drawable defaultMarker, Context context, int textsize) {
		  this(defaultMarker);
		  this.context = context;
		  this.mTextSize = textsize;
	}
	
	
	@Override
	protected MyOverlayItem createItem(int i) {
		return mapOverlays.get(i);
	}

	@Override
	public int size() {
		return mapOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
		MyOverlayItem item = mapOverlays.get(index);
		
		// Log.i("ItemizedOverlay_OnTap", item.getSnippet()+": "+item.getTitle()) ;
		// Toast.makeText(context, item.getSnippet(), Toast.LENGTH_SHORT).show();
		
		// testiramo sledece: ako je item kliknut, aktiviramo neku f-ju u mapactivity
/*		
		OnlineMapActivity ma = (OnlineMapActivity) context;
		// NAPOMENA: Ovdje bi trebalo asynchron-o poslati poruku parent activity-ju
		// jer u slucaju da activity onda mijenja overlay listu, ovo moze da krahira!!!
		// ma.TestFunc(item.getSnippet());
        
		// If municipals are displayed, draw all categories for clicked municipal
        if (OnlineMapActivity.drawMode == OnlineMapActivity.mapmodeEnum.DRAW_ALL_MUNICIPALS.getValue()) {
        	// Log.i("OnTap", item.catID+": "+item.cityID) ;
        	ma.drawCategoryForOneMunicipal(0, item.cityID);
        }
        else if (OnlineMapActivity.drawMode == OnlineMapActivity.mapmodeEnum.DRAW_ALL_MUNICIPALS.getValue()) {
        	
        }

*/
		OnlineMapActivity ma = (OnlineMapActivity) context;
    	ma.processInfoDialog(item.locID, item.catID, item.cityID, item.point);

		return true;
	}
	
	public void addOverlay(MyOverlayItem overlay) {
		mapOverlays.add(overlay);
	    this.populate();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		// TODO Auto-generated method stub
		//return super.onTouchEvent(event, mapView);
		/*
        //---when user lifts his finger---
        if (event.getAction() == 1) {                
            GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());

            Toast.makeText(context, p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show();

            Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);

                String add = "Adresa: ";
                if (addresses.size() > 0) {
                    for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                       add += addresses.get(0).getAddressLine(i) + "\n";
                }

                Toast.makeText(context, add, Toast.LENGTH_SHORT).show();
            }
            catch (IOException e) {                
                //Toast.makeText(getBaseContext(), "Greska!", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }   
            return true;
        }
        else                
        
		 */
		
		/*
		 * VAZNO: Najpre ide OnTouch DOWN event, zatim OnLongClick (iz View klase), zatim OnTouch UP event.
		 * Sada, ako ovaj listener vraca super.OnTouch ili false, onda ce slijediti onTap event.
		 * Ukoliko pak ovaj listener vraca true, onda ne slijedi onTap event!!!
		 */
		// Log.i("ItemizedOverlay_OnTouch", "action: "+event.getAction()) ;
        return  super.onTouchEvent(event, mapView);
	}

    @Override 
    public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) 
    { 
        super.draw(canvas, mapView, shadow); 
 
        if (shadow == false) 
        { 
            //cycle through all overlays 
            for (int index = 0; index < mapOverlays.size(); index++) 
            { 
                OverlayItem item = mapOverlays.get(index); 
 
                // Converts lat/lng-Point to coordinates on the screen 
                GeoPoint point = item.getPoint(); 
                Point ptScreenCoord = new Point() ; 
                mapView.getProjection().toPixels(point, ptScreenCoord); 
 
                //Paint 
                Paint paint = new Paint(); 
                paint.setTextAlign(Paint.Align.CENTER); 
                paint.setTextSize(mTextSize); 
                paint.setARGB(200, 0, 0, 0); // alpha, r, g, b (Black, semi see-through) 
 
                //show text to the right of the icon 
                canvas.drawText(item.getTitle(), ptScreenCoord.x, ptScreenCoord.y+mTextSize, paint); 
                this.populate();		// we need something to invalidate view!!!!!!!!
            } 
        } 
    } 

}
